/**
 * 微信 主应用入口
 *
 */
define(["public/app.min"], function () {

    var self,
        app = win.app,
        types = [],
        units = [],//单位
        zhiwei = [],//职位
        juese = [],//角色
        menus = [],
        others = {
            hasgwc: true
        },
        statsSJ = {
            A: 0, B: 0, C: 0
        }
    ;

    function getUserInfoBack() {

        menus.pushArray(app.userInfos.auths, true);

        menus.iFilter({
            o: {
                permission_: 'wx0006'
            },
            cb: function (arr, i) {
                if (i > 0) {
                    others.hasgwc = true;
                    self.getType();
                }
            }
        });
    }

    app.newModule({
        init: function () {
            self = this;

            self._super({
                _tag: 'p_main',
                _readyBefore: function () {
                    //最先执行
                    self.mData = {
                        authData: [],
                        obj: {
                            dqs: 0,
                            wfh: 0
                        }
                    };

                    app.mainPage = {};

                    // 用户数据 放到main对象里面  废弃
                    self.userInfo = {
                        ID: app.cookie('_WX_ACCOUNT_'),
                        NAME: app.cookie('_WX_USERNAME_'),
                        MOBILE: app.cookie('_WX_MOBILE_'),
                        SID: app.cookie('_WX_ID_'),
                        ORGID: app.cookie('_WX_ORGID_'),
                    };

                    self.zhiwei = zhiwei;//职位
                    self.juese = juese;//角色
                    self.initFilters();

                }
            }, win);

        },
        statsSJ: function () {
            app.ajax({
                xurl: "api/wx/gwaq554/statsSJ",
                cb: function (result) {
                    if (result.ok) {
                        var res = result.data.data;
                        if (res.GWAQ554.GWCS039) {
                            res.GWAQ554.GWCS039 = (res.GWAQ554.GWCS039 / 10000).toFixed(2);
                            statsSJ['A'] = res.GWAQ554.GWCS039;
                        }

                        if (res.GWAQ555.GWBK005) {
                            res.GWAQ555.GWBK005 = (res.GWAQ555.GWBK005 / 10000).toFixed(2);
                            statsSJ['B'] = res.GWAQ555.GWBK005;
                        }

                        statsSJ['C'] = res.GWAQ556;
                    }
                }
            });
        },
        setLocation: function () {
            let latlng = app.wxLocationCache();
            if (latlng.lat) {
                app.lnglatConvert({
                    lat: latlng.lat,// 纬度
                    lng: latlng.lng,// 经度
                    cb: function (lnglat) {
                        app.ajax({
                            xurl: "api/wx/gwa/setLocation",
                            xdata: {
                                GWAA020: lnglat.lat,// 纬度
                                GWAA019: lnglat.lng// 经度
                            }
                        });
                    }
                });
            }
        },
        getZhiwei: function () {
            app.ajax({
                xurl: "api/wx/gwa/getZw",
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        zhiwei.pushArray(data.data, true);
                    }
                }
            });
        },
        getJuese: function () {
            app.ajax({
                xurl: "api/wx/gwa/getJs",
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        juese.pushArray(data.data, true);
                    }
                }
            });
        },
        getUnits: function () {
            app.ajax({
                xurl: "api/wx/gwa/getGwablList",
                cb: function (result) {
                    if (result.ok) {
                        units.pushArray(result.data.data);
                    }
                }
            });
        },
        getType: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwahData",
                cb: function (result) {
                    if (result.ok) {
                        types.pushArray(result.data.data);
                    }
                }
            });
        },
        dataDrawing: function () {
            //执行数据渲染
            new Vue({
                el: '#content',
                data: {
                    others: others,
                    menus: menus,
                    items: types,
                    statsSJ: statsSJ
                },
                methods: {
                    openWindow: function (code_, url_) {
                        if (code_ == 'wx0006') {//购物车下单
                            delete app.pageParameters['classes_page'];
                        }

                        app.openPage({
                            src: url_ + '.html'
                        });
                    },
                    openFoot: function (t) {
                        if (t == 'fl') {
                            app.openPage({
                                src: "gwcxd/classes.html"
                            });
                        } else if (t == 'gwc') {
                            app.openPage({
                                src: "gwcxd/gwc.html",
                                argsKey: 'classes_page', //替换已经有的参数为空

                            });
                        } else if (t == 'wo') {
                            app.openPage({
                                src: "me/me.html"
                            });
                        }
                    },
                    openTypes: function (t) {
                        app.openPage({
                            src: "gwcxd/classes.html",
                            argsKey: 'classes_page',
                            args: {
                                type: t
                            }
                        });
                    },
                    openSjmx: function (GWBH031) {
                        if (GWBH031 == 'A') {
                            app.openPage({
                                src: 'gwaq554/gwaq554.html',
                                args: {
                                    GWBH031: GWBH031
                                }
                            });
                        } else if (GWBH031 == 'B') {
                            app.openPage({
                                src: 'gwaq555/gwaq555.html',
                                args: {
                                    GWBH031: GWBH031
                                }
                            });
                        } else if (GWBH031 == 'C') {
                            app.openPage({
                                src: 'gwaq556/gwaq556.html',
                                args: {
                                    GWBH031: GWBH031
                                }
                            });
                        }
                    }
                }
            });
        },
        initFilters: function () {
            // 声明全局过滤器
            Vue.filter('MMRD001STATUS', function (t) {
                return win.eval({'1': '安装', '2': '维修', '3': '保养'})[t];
            });

            Vue.filter('MMRD019STATUS', function (t) {
                return win.eval({'1': '紧急', '2': '一般'})[t];
            });

            Vue.filter('sexStatus', function (t) {
                return win.eval({'0': '未知', '1': '男', '2': '女'})[t];
            });

            Vue.filter('MMRD020STATUS', function (t) {
                return win.eval({'1': '发起', '2': '受理', '3': '派工', '4': '报工', '5': '评价', '6': '回访结案'})[t];
            });

            Vue.filter('GWBJ005STATUS', function (t) {
                return win.eval({'1': '主要对手', '2': '重要对手', '3': '一般对手', '4': '其他对手'})[t];
            });

            Vue.filter('goodsUnits', function (t) {
                var a = units.filterKV({'GWABL001': t});
                if (a.length > 0) {
                    return a[0].GWABL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('zhiwei', function (t) {
                var a = zhiwei.filterKV({'GWBD001': t});
                if (a.length > 0) {
                    return a[0].GWBDL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('juese', function (t) {
                var a = juese.filterKV({'GWAW001': t});
                if (a.length > 0) {
                    return a[0].GWAWL003;
                } else {
                    return '未知';
                }
            });

            Vue.filter('goodsImgsUtils', function (name) {
                if (name) {
                    //http://localhost:8083
                    var origin = location.origin;
                    if (origin.indexOf('localhost') == -1) {
                        return location.origin + '/wissun-smfile/' + name.split(",")[0];
                    } else {
                        return 'http://fx.digiwin.com:9017/wissun-smfile/' + name.split(",")[0];
                    }
                } else {
                    return '../../../img/wx/images.png';
                }
            });

            Vue.filter('orderImgsUtils', function (name) {
                if (name) {
                    if (name.indexOf('wx-uploads') != -1) {
                        return app.cxt() + name;
                    } else {
                        // return app.cxt() + 'wx-uploads/imgs/' + name;
                        return "E:\\projs\\mingyi\\crm6.0\\crm-wissun-upload\\"+name;
                    }
                } else {
                    return '../../static/img/images.png';
                }
            });

            Vue.filter('dateFmt', function (date) {
                if (date) {
                    return date.replace(" 00:00:00","")
                }
                return ""
            });
        },
        _ready: function () {

            if (app.userInfos) {
                getUserInfoBack();
            } else {
                app.getUserInfoBack = function () {
                    getUserInfoBack();
                };
            }

            app.getImgUrl = function(){
                var origin = location.origin;
                if (origin.indexOf('localhost') == -1) {
                    return location.origin + '/upload/images/products/';
                } else {
                    return 'http://fx.digiwin.com:9017/wissun-smfile/';
                }
            };

            self.dataDrawing();
            self.statsSJ();
            self.getUnits();
            self.getJuese();
            self.getZhiwei();

            // setTimeout(function () {
            //     self.setLocation();
            // }, 10000);

        },
        _lazy: function () {
            this._super();
            require(['wx-iconfont'], $.noop);

            mui("#slider").slider({
                interval: 2000
            });

        }
    });

});